package com.myteam.fugaocrm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.myteam.fugaocrm.modle.FgCustomer;
import com.myteam.fugaocrm.modle.FgCustomerCompany;
import com.myteam.fugaocrm.service.CustomerCompanyServiceImpl;
import com.myteam.fugaocrm.service.CustomerServiceImpl;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 客户联系人Controller
 * @author BZX
 *
 */
@Controller
public class ContactController {
	@Resource protected CustomerServiceImpl customerServiceImpl;
	@Resource protected CustomerCompanyServiceImpl customerCompanyServiceImpl;
	
	@RequestMapping("customer/all")
	@ResponseBody
	public ModelAndView all(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<FgCustomer> fgCustomerList = customerServiceImpl.all();
		
		List<FgCustomerCompany> fgCustomerCompanyList = customerCompanyServiceImpl.all();
		request.getSession().setAttribute("fgCustomerCompanyList", fgCustomerCompanyList);
		
		ModelAndView view = new ModelAndView();
		view.addObject("fgCustomerList", fgCustomerList);
		view.setViewName("arch_customer_contacts.jsp");
		return view;
	}
	
	@RequestMapping("customer/delete")
	@ResponseBody
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer customerId = Integer.parseInt(request.getParameter("customerId").toString());
		
		customerServiceImpl.deleteOne(customerId);
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/customer/all");
		return view;
	}
	
	@RequestMapping("contact/insertorupdate")
	@ResponseBody
	public ModelAndView insertOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String customerIdStr = request.getParameter("customerId")+"";
		String customerName = request.getParameter("customerName")+"";
		String title = request.getParameter("title")+"";
		String customerMobile = request.getParameter("customerMobile")+"";
		String customerEmail = request.getParameter("customerEmail")+"";
		String customerCompanyId = request.getParameter("customerCompanyId")+"";
		
		//校验
		
		
		//插入或更新
		Date date = new Date();
		String staffName = request.getSession().getAttribute("staffName")+"";
		Map<String, Object> params = new HashMap<String, Object>();
		
		if("".equals(customerIdStr)) {
			//insert
			params.put("customerName", customerName);
			params.put("title", title);
			params.put("customerMobile", customerMobile);
			params.put("customerEmail", customerEmail);
			params.put("customerCompanyId", customerCompanyId);
			params.put("createdUser", staffName);
			params.put("createdDate", date);
			customerServiceImpl.insert(params);
		}else {
			//update
			Integer customerId = Integer.parseInt(customerIdStr);
			params.put("customerId", customerId);
			params.put("customerName", customerName);
			params.put("title", title);
			params.put("customerMobile", customerMobile);
			params.put("customerEmail", customerEmail);
			params.put("customerCompanyId", customerCompanyId);
			params.put("modifiedDate", date);
			params.put("modifiedUser", staffName);
			customerServiceImpl.update(params);
		}
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/customer/all");
		return view;
	}
}
